<template>
  <div>
    <div>
      <!-- {{ orders }} -->

      <div v-for="o in orders" :key="o.id">
        {{ o.id }}
        <van-button size="small" @click.native="handleOrderClick(o)">
          显示
        </van-button>
      </div>

      <van-cell-group>
        <van-cell title="订单日期:" :value="order.date_order" />
        <van-cell title="订单编号:" :value="order.name" />
        <van-cell title="大夫:" :value="order.partner_id__name" />
        <van-cell title="咨询费:" :value="order.amount_total" />
        <van-cell title="订单状态:" :value="order.state_name" />
        <van-cell title="开票状态:" :value="order.invoice_status_name" />
        <van-cell title="发票编号:" :value="invoice.name" />
        <van-cell title="发票状态:" :value="invoice.state_name" />
        <van-cell
          title="付款状态:"
          :value="invoice.invoice_payment_state_name"
        />
      </van-cell-group>
      <van-button
        v-if="order.state === 'draft'"
        size="small"
        @click.native="handleOrderConfirm"
      >
        订单确认
      </van-button>
      <van-button
        v-if="order.invoice_status === 'to invoice'"
        size="small"
        @click.native="handleOrderToInvoice"
      >
        订单开票
      </van-button>
      <van-button
        v-if="invoice.state === 'draft'"
        size="small"
        @click.native="handleInvoicePost"
      >
        发票确认
      </van-button>
    </div>
  </div>
</template>

<script>
import { Dialog } from 'vant'

export default {
  name: 'PO',
  components: { [Dialog.Component.name]: Dialog.Component },
  directives: {},
  filters: {},
  props: {},

  data() {
    return {
      //
    }
  },
  computed: {
    isCustomer: function() {
      return !this.$store.state.user.isEmployee
    },
    isEmployee: function() {
      return this.$store.state.user.isEmployee
    },

    isInvoiceToPay: function() {
      const invoice = this.invoice
      return (
        invoice.state === 'posted' &&
        invoice.invoice_payment_state === 'not_paid'
      )
    },

    invoice: function() {
      const inv = this.$store.state.AccountMove.dataDict
      const states = { draft: '草稿', posted: '已确认' }

      const invoice_payment_states = {
        not_paid: '未付款',
        paid: '已支付'
      }

      return {
        ...inv,
        state_name: states[inv.state] || inv.state,
        invoice_payment_state_name:
          invoice_payment_states[inv.invoice_payment_state] ||
          inv.invoice_payment_state
      }
    },

    orders: function() {
      return this.$store.state.PurchaseOrder.dataList
    },

    order: function() {
      const order = this.$store.state.PurchaseOrder.dataDict
      const states = { draft: '草稿', purchase: '确认', done: '已完成' }
      const invoice_statuses = {
        no: '无',
        'to invoice': '待开票',
        invoiced: '已开票'
      }

      return {
        ...order,
        id: order.id,
        // analytic_account_partner_id__name: order.analytic_account_partner_id__name,
        state_name: states[order.state] || order.state,
        invoice_status_name:
          invoice_statuses[order.invoice_status] || order.invoice_status
      }
    }
  },
  async created() {
    const { id } = this.$route.query

    if (id) {
      await this.$store.dispatch('PurchaseOrder/browse_one', { id })
    } else {
      await this.$store.dispatch('PurchaseOrder/search')
    }

    console.log('PurchaseOrder,', this.order)
    // console.log('Invoice,', this.invoice)
  },

  methods: {
    async handleOrderClick(o) {
      console.log('handleOrderClick,', o.id)
      await this.$store.dispatch('PurchaseOrder/browse_one', { id: o.id })
    },

    async handleOrderConfirm() {
      console.log('handleOrderConfirm,', this.order.state)
      if (this.order.state === 'draft') {
        await this.$store.dispatch('PurchaseOrder/action_confirm', {
          id: this.order.id
        })
      }
    },

    async handleOrderToInvoice() {
      console.log('handleOrderToInvoice,', this.order.invoice_status)
      if (this.order.invoice_status === 'to invoice') {
        await this.$store.dispatch('PurchaseOrder/create_invoices', {
          id: this.order.id
        })
      }
    },

    async handleInvoicePost() {
      console.log('handleInvoicePost,', this.invoice.state)
      if (this.invoice.state === 'draft') {
        await this.$store.dispatch('AccountMove/action_post', {
          id: this.invoice.id
        })
      }
    }
  }
}
</script>

<style type="text/css"></style>
